/*
 * FileName：PaperQuestionServiceImpl.java 
 * <p>
 * Copyright (c) 2017-2020 <a href="https://www.smartwx.info">hermit(1154808491@qq.com)</a>.
 * <p>
 * Licensed under the GNU General Public License, Version 3 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.gnu.org/licenses/gpl-3.0.html
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package com.smart.wx.wxcms.service.impl;

import javax.annotation.Resource;

import com.smart.common.Constants;
import com.smart.common.util.StringUtil;
import com.smart.common.util.Util;
import com.smart.wx.wxcms.dao.PaperInfoMapper;
import com.smart.wx.wxcms.dao.PaperOptionMapper;
import com.smart.wx.wxcms.entity.PaperInfo;
import com.smart.wx.wxcms.entity.PaperOption;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import com.smart.wx.wxcms.dao.PaperQuestionMapper;
import com.smart.wx.wxcms.entity.PaperQuestion;
import com.smart.wx.wxcms.service.PaperQuestionService;

/**
 * 问卷调查问题表
 *
 * @author hermit
 * @version 2.0
 * @date 2018-07-23 21:22:41
 */
@Service
public class PaperQuestionServiceImpl implements PaperQuestionService {


    @Resource
    private PaperInfoMapper paperInfoDao;

    @Resource
    private PaperQuestionMapper paperQuestionDao;

    @Resource
    private PaperOptionMapper paperOptionDao;


    @Override
    public PaperQuestion queryObject(PaperQuestion paperQuestion) throws Exception {
        return paperQuestionDao.queryObject(paperQuestion);
    }

    @Override
    public List<PaperQuestion> queryList(PaperQuestion paperQuestion) throws Exception {
        return paperQuestionDao.queryList(paperQuestion);
    }

    @Override
    public List<PaperQuestion> queryListByPage(PaperQuestion paperQuestion) throws Exception {


        if (Constants.ROOT_NODE_ID.equals(paperQuestion.getPaperInfoId()) || paperQuestion.getPaperInfoId() == null || paperQuestion.getPaperInfoId().equals("")) {
            paperQuestion.setPaperInfoId("");

            PaperInfo paperInfo = new PaperInfo();
            paperInfo.setAccount(paperQuestion.getAccount());
            List<PaperInfo> list = this.paperInfoDao.queryList(paperInfo);
            paperQuestion.setPaperInfoIdList(this.getPaperInfoIdList(list));
        }

        return paperQuestionDao.queryListByPage(paperQuestion);
    }

    @Override
    public Integer queryCount(PaperQuestion paperQuestion) throws Exception {
        Integer ct = paperQuestionDao.queryCount(paperQuestion);
        return ct == null ? 0 : ct;
    }

    @Override
    public Integer insert(PaperQuestion paperQuestion) throws Exception {


        /************* STEP_1: 新增 fxy_exam_question 记录 ***********************/
        paperQuestion.setId(Util.getUUID());
        paperQuestion.setCreateTime(new Date());
        paperQuestion.setUpdateTime(new Date());
        paperQuestionDao.insert(paperQuestion);

        /************* STEP_2: 新增 fxy_exam_option 记录 ***********************/
        this.addOption(paperQuestion);

        return 1;
    }


    private void addOption(PaperQuestion paperQuestion) throws Exception {

        List<PaperOption> optionArr = paperQuestion.getOption();
        if (optionArr != null && optionArr.size() > 0) {

            for (int i = 0; i < optionArr.size(); i++) {

                int order = i + 1;
                PaperOption tempBean = optionArr.get(i);

                String optionContent = tempBean.getOptionContent();

                PaperOption paperOption = new PaperOption();

                paperOption.setId(Util.getUUID());
                paperOption.setAccount(paperQuestion.getAccount());
                paperOption.setPaperQuestionId(paperQuestion.getId());
                paperOption.setOptionOrder(order);
                paperOption.setOptionContent(optionContent);
                paperOption.setCreateTime(new Date());
                paperOption.setUpdateTime(new Date());

                paperOptionDao.insert(paperOption);
            }
        }
    }


    @Override
    public Integer update(PaperQuestion paperQuestion) throws Exception {

        /************* STEP_1: 修改 fxy_exam_question 记录 ***********************/

        paperQuestion.setUpdateTime(new Date());
        this.paperQuestionDao.update(paperQuestion);

        List<PaperOption> optionArr = paperQuestion.getOption();
        if (optionArr != null && optionArr.size() > 0) {

            for (int i = 0; i < optionArr.size(); i++) {

                PaperOption tempBean = optionArr.get(i);

                String optionId = tempBean.getId();
                String optionContent = tempBean.getOptionContent();

                PaperOption fxyExamOption = new PaperOption();

                fxyExamOption.setId(optionId);
                fxyExamOption.setOptionContent(optionContent);
                fxyExamOption.setUpdateTime(new Date());

                paperOptionDao.update(fxyExamOption);
            }
        }

        return 1;
    }

    @Override
    public Integer delete(PaperQuestion paperQuestion) throws Exception {
        String paperQuestionId = paperQuestion.getId();
        if (StringUtil.isNotBlank(paperQuestionId)) {
            List<String> list = Arrays.asList(paperQuestionId.split(","));
            this.paperQuestionDao.deleteBatch(list);
            this.paperOptionDao.deleteBatchByQuestionId(list);
        }
        return 1;
    }

    @Override
    public Integer deleteBatch(List<String> ids) throws Exception {
        return paperQuestionDao.deleteBatch(ids);
    }


    private List<String> getPaperInfoIdList(List<PaperInfo> list) {
        List<String> res = new ArrayList<String>();
        if (list != null && list.size() > 0) {
            for (PaperInfo paperInfo : list) {
                res.add(paperInfo.getId());
            }
        }
        return res;
    }


}
